package com.smbms.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smbms.entity.Bill;
import com.smbms.entity.Provider;
import com.smbms.entity.User;
import com.smbms.service.bill.BillService;
import com.smbms.service.provider.ProviderService;
import com.smbms.tool.PageSupport;

@Controller
@RequestMapping("bill")
public class BillController {
	
	
	@Resource
	private BillService billService;
	
	@Resource
	private ProviderService providerService;
	
	/**
	 * 分页显示订单列表
	 * @param queryProductName
	 * @param queryProviderId
	 * @param queryIsPayment
	 * @param pageIndex
	 * @param model
	 * @return
	 */
	@RequestMapping("list")
	public String list(String queryProductName,String queryProviderId,String queryIsPayment,String pageIndex,Model model){
		
		String productName = "";
		if(queryProductName!=null){
			productName = queryProductName;
		}
		
		int providerId = 0;
		if(queryProviderId!=null){
			providerId = Integer.parseInt(queryProviderId);
		}
		int isPayment = 0;
		if(queryIsPayment!=null){
			isPayment = Integer.parseInt(queryIsPayment);
		}
		
		int currentPageNo=1;//当前页码
    	if(pageIndex!=null){
    		currentPageNo=Integer.valueOf(pageIndex);
    	}
		
		Integer pageSize = 5;
		
		Integer fromIndex=(currentPageNo-1)*pageSize;//起始数字
		
		List<Provider> providers = providerService.findProviderAll();
		model.addAttribute("providerList",providers);
		
		int totalCount = billService.count(productName, providerId, isPayment);
		
		PageSupport pageSupport = new PageSupport();
		pageSupport.setTotalCount(totalCount);
		pageSupport.setPageSize(pageSize);
		pageSupport.setCurrentPageNo(currentPageNo);
		
		int totalPageCount = pageSupport.getTotalPageCount();
		
		model.addAttribute("totalPageCount",totalPageCount);
		model.addAttribute("totalCount",totalCount);
		model.addAttribute("currentPageNo",currentPageNo);
		
	
		
		
		List<Bill> bills = billService.getBillList(productName, providerId, isPayment, fromIndex, pageSize);
		model.addAttribute("queryProductName",queryProductName);
		model.addAttribute("queryProviderId",providerId);
		model.addAttribute("queryIsPayment",isPayment);
		model.addAttribute("billList",bills);
		return "billlist";
	}
	
	/**
	 * 去到添加订单页面
	 * @return
	 */
	@RequestMapping("toAdd")
	public String toAdd(){
		return "billadd";
	}
	
	/**
	 * 添加订单
	 * @param bill
	 * @param request
	 * @return
	 */
	@RequestMapping("add")
	public String add(Bill bill,HttpServletRequest request){
		User user = (User)request.getSession().getAttribute("userSession");
		bill.setCreatedBy(user.getId());
		bill.setCreationDate(new Date());
		boolean flag = billService.add(bill);
		if(flag){
			return "redirect:list";
		}else{
			return "billadd";
		}
	}
	
	/**
	 * 去到修改订单页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("toModify")
	public String toModify(String id,Model model){
		Integer idInt = Integer.parseInt(id);
		Bill bill = billService.getBillById(idInt);
		model.addAttribute(bill);
		return "billmodify";
	}
	
	/**
	 * 修改订单
	 * @param bill
	 * @param request
	 * @return
	 */
	@RequestMapping("modify")
	public String modify(Bill bill,HttpServletRequest request){
		User user = (User)request.getSession().getAttribute("userSession");
		bill.setModifyBy(user.getId());
		bill.setModifyDate(new Date());
		boolean flag = billService.update(bill);
		if(flag){
			return "redirect:list";
		}else{
			return "billadd";
		}
	}
	
	/**
	 * 去到显示订单页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("toView")
	public String toView(String id,Model model){
		Integer idInt = Integer.parseInt(id);
		Bill bill = billService.getBillById(idInt);
		model.addAttribute(bill);
		return "billview";
	}
	
	/**
	 * 删除订单
	 * @param id
	 * @return
	 */
	@RequestMapping("delete")
	@ResponseBody
	public Object delete(String id){
		Integer idInt = Integer.parseInt(id);
		boolean flag = billService.delete(idInt);
		return flag;
	}

}
